/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.mapagay.conexao;

import br.com.mapagay.configuracoes.CFG;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lauro
 */
public class Conexao {

    private static ComboPooledDataSource dataSource;

    public static synchronized Connection getConexao() {
        try {
            if (dataSource == null) {
                dataSource = new ComboPooledDataSource();
                dataSource.setDriverClass("com.mysql.jdbc.Driver");
                dataSource.setJdbcUrl(CFG.URL_BD);
                dataSource.setUser(CFG.USUARIO_BD);
                dataSource.setPassword(CFG.SENHA_BD);
                dataSource.setMinPoolSize(1);
                dataSource.setMaxPoolSize(50);
                dataSource.setMaxIdleTime(300);
                dataSource.setAcquireIncrement(1);
                //dataSource.setAutomaticTestTable();
                dataSource.setCheckoutTimeout(5000);
            }
            return dataSource.getConnection();
        } catch (SQLException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void close(Connection conn) {
        close(conn, null, null);
    }

    public static void close(Connection conn, Statement st) {
        close(conn, st, null);
    }

    public static void close(Connection conn, Statement st, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
